<template>
  <div class="minegoods-model">
    <div class="date-box">
      <div class="date-select" @click="showDateSelect">
        <div class="time">{{dateTimeVal | fmate}}</div>
        <div class="icon" :class="{'arrow': showDate}"></div>
      </div>
    </div>
    <div class="scroll-container">
      <scroller :on-refresh="refresh" :on-infinite="infinite" v-if="!isInit">
        <div class="record-list" v-if="Object.keys(allCoupons).length" :data-length="allDataNum"
          :class="{'loaded-data': allDataNum < 5, 'loadmore-data': allDataNum < 10}">
          <div class="record-cate" v-for="(value, key) in allCoupons" :key="key">
            <div class="date">{{key}}</div>
            <div class="record-item">
              <div class="contents" v-for="item in value" :key="item.user_coupon_id">
                <div class="baseinfo">
                  <img class="iconimg" src="../../../assets/imgs/mine/yuanyouhuiquan@2x.png" alt="">
                  <div class="types">
                    <div class="type">{{item.title}}</div>
                    <div class="time" v-if="item.status==1">到期时间：{{item.expire_time}}</div>
                    <div class="time" v-else>兑换时间：{{item.use_time}}</div>
                  </div>
                </div>
                <div class="unuse" v-if="item.status==1">{{item.status_name}}</div>
                <div class="use" v-else>{{item.status_name}}</div>
              </div>
              <!-- <div class="status">{{item.status}}</div> -->
              <!-- <div class="unuse" v-if="item.status==1">{{item.status_name}}</div>
              <div class="use" v-else>{{item.status_name}}</div> -->
            </div>
          </div>
          <!-- <div class="record-item" v-for="(item, index) in 25" :key="index">
            <div class="types">
              <div class="type">签到</div>
              <div class="time">12月15日 20:57</div>
            </div>
            <div class="money">+ 20</div>
          </div> -->
        </div>
      </scroller>
    </div>
    <div class="no-datas" v-if="!loading && !Object.keys(allCoupons).length">
      <img src="../../../assets/imgs/icon-noattention@2x.png" alt="">
      <div class="desc">暂无数据</div>
    </div>
    <datetime
      :show="showDate"
      v-model="dateTimeVal"
      format="YYYY-MM"
      start-date="2018-01-01"
      :end-date="currentDate"
      @on-confirm="dateConfirm"
      @on-cancel="dateCancel"
      year-row="{value}年"
      month-row="{value}月"
    ></datetime>
    <popup v-model="showPopup">
      <div class="record-type">
        <div class="header">
          选择类型
          <span class="icon-del" @click="showPopup=false"></span>
        </div>
        <div class="types">
          <div class="state" v-for="(item, index) in  recordTypes" :key="index"
          :class="{'on':recordType==index}" @click="selectType(item.status, index)">{{item.text}}</div>
        </div>
        <div class="btn" @click="searchRecord">确定</div>
      </div>
    </popup>
  </div>
</template>
<script>
import { mapActions } from 'vuex';

export default {
  components: {
  },
  data () {
    return {
      pageNum: 1,
      isPull: false, // 下拉
      loading: true,
      isInit: false, // 重置滚动
      allDataNum: 0,
      showDate: false,
      showPopup: false,
      dateTimeVal: '',
      recordType: 0,
      recordTypes: [
        {
          id: 1,
          status: '',
          text: '全部状态'
        },
        {
          id: 2,
          status: 1,
          text: '支付成功'
        },
        {
          id: 3,
          status: 3,
          text: '处理中'
        }
      ],
      status: '',
      allCoupons: {
        // "03月24日": [
        //   {
        //       "user_coupon_id": "1",
        //       "status": "1",
        //       "discount": 0,
        //       "title": "5%抵扣券",
        //       "expiration_time": "永久有效",
        //       "use_time": "",
        //       "status_name": "未使用"
        //   },
        //   {
        //       "user_coupon_id": "2",
        //       "status": "2",
        //       "discount": 0,
        //       "title": "10%抵扣券",
        //       "expiration_time": "永久有效",
        //       "use_time": "",
        //       "status_name": "已使用"
        //   },
        //   {
        //       "user_coupon_id": "3",
        //       "status": "3",
        //       "discount": 0,
        //       "title": "20%抵扣券",
        //       "expiration_time": "永久有效",
        //       "use_time": "",
        //       "status_name": "已失效"
        //   }
        // ]
      }
    }
  },
  computed: {
    currentDate () {
      const date = new Date();
      const year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      month = month < 10 ? '0' + month : month;
      day = day < 10 ? '0' + day : day;
      return `${year}-${month}-${day}`
    }
  },
  filters: {
    fmate (val) {
      const _date = val.split('-');
      return `${_date[0]}年${_date[1]}月`;
    }
  },
  created () {
    this.dateTimeVal = this.currentDate.substring(0, 7);
  },
  methods: {
    ...mapActions({
      getCouponsList: 'mine/getCouponsList'
    }),
    refresh (done) { // 下拉
      this.isPull = true;
      this.pageNum = 1;
      this.loading = true;
      this.getDatas(done);
    },
    infinite (done) { // 上滑
      if (!this.isPull) {
        this.loading = true;
        this.getDatas(done);
      }
    },
    getDatas (done) {
      if (this.loading) {
        const params = {
          month: this.dateTimeVal.replace(/-/gi, ''), // 月份
          type: '', // 类型  1未使用 2已过期
          page: this.pageNum,
          size: 20
        }
        this.getCouponsList(params).then(res => {
          this.loading = false;
          let listData = res.list || {}; // 新接口数据
          if (this.isInit) {
            this.isInit = false;
          }
          if (Object.keys(listData).length) {
            if (this.isPull) {
              this.isPull = false;
              this.allCoupons = {};
            }
            this.pageNum += 1;
            let listDatas = this.allCoupons; // 已经存在的数据
            let concatDatas = {};
            let datalen = 0;
            for (let i in listData) {
              if (listDatas[i]) {
                datalen += (listDatas[i].length + listData[i].length);
                concatDatas[i] = [...listDatas[i], ...listData[i]]
              } else {
                datalen += listData[i].length;
                concatDatas[i] = listData[i];
              }
            }
            this.allDataNum = datalen;
            this.allCoupons = concatDatas;
            if (this.pageNum < res.page.total) {
              done(true);
            } else {
              done(2); ;
            }
          } else {
            done(2);
          }
        });
      }
    },
    selectType (status, index) {
      this.status = status;
      this.recordType = index;
    },
    searchRecord () {
      console.log(this.recordType);
      // 成功...
      this.showPopup = false;
      this.allCoupons = {};
      this.isInit = true;
      this.refresh();
    },
    showDateSelect () {
      this.showDate = true;
    },
    dateConfirm () {
      this.showDate = false;
      this.allCoupons = {};
      this.isInit = true;
      this.refresh();
      // this.getCouponList();
      // console.log('dateTimeVal', this.dateTimeVal)
    },
    dateCancel () {
      this.showDate = false;
    }
  }
}
</script>
<style lang="less" scoped>
  @import '../../../assets/styles/mixins.less';
  .minegoods-model {
    width: 100%;
    position: absolute;
    top: 1.72rem;
    bottom: 0;
    background: #f7f7f7;
    overflow: hidden;
    // background: #ffffff;
    // .date-box {
    //   position: fixed;
    //   top: 1.72rem;
    //   width: 100%;
    //   // height: 1.06rem;
    //   padding: 0.14rem 0.2rem;
    //   box-sizing: border-box;
    //   background: #f7f7f7;
    //   z-index: 10;

    //   .allmoney {
    //     margin-top: 0.1rem;
    //     font-size: 0.22rem;
    //     color: #999999;
    //   }
    // }
    .date-select {
        display: inline-flex;
        align-items: center;
        margin-left: 0.2rem;
        height: 0.66rem;
        line-height: 0.4rem;
        box-sizing: border-box;
        .time {
          font-size: 0.28rem;
          color: #333333;
        }
        .icon {
          width: 0.24rem;
          height: 0.13rem;
          margin-left: 0.05rem;
          background-size: 0.24rem 0.13rem;
          background-repeat: no-repeat;
          background-image: url('../../../assets/imgs/mine/icon-down@2x.png')
        }
        .arrow {
          transform: rotate(180deg);
          transition: transform 0.4s;
        }
      }

    .date-box {
      width: 100%;
      height: 0.66rem;
      box-sizing: border-box;
      background: #f7f7f7;
      .date-select {
        display: inline-flex;
        align-items: center;
        margin-left: 0.2rem;
        height: 0.66rem;
        line-height: 0.4rem;
        box-sizing: border-box;
        .time {
          font-size: 0.28rem;
          color: #333333;
        }
        .icon {
          width: 0.24rem;
          height: 0.13rem;
          margin-left: 0.05rem;
          background-size: 0.24rem 0.13rem;
          background-repeat: no-repeat;
          background-image: url('../../../assets/imgs/mine/icon-down@2x.png')
        }
        .arrow {
          transform: rotate(180deg);
          transition: transform 0.4s;
        }
      }
    }

    .scroll-container {
      position: absolute;
      top: 0.66rem;
      bottom: 0;
      width: 100%;
      overflow-y: auto;
      // position: relative;
      // // top: 0.88rem;
      // margin-bottom: 1rem;
      background: #f7f7f7;
    }
    .record-list {
      // position: absolute;
      // top: 0.66rem;
      // bottom: 0;
      // width: 100%;
      // overflow-y: auto;
      // position: relative;
      // // top: 0.88rem;
      // top: 0;
      // background: #ffffff;
      .record-cate {
        .date {
          line-height: 0.6rem;
          padding-left: 0.3rem;
          font-size: 0.24rem;
          color: #666666;
          background: #f7f7f7;;
        }
      }
      .record-item {
        position: relative;
        // height: 0.96rem;
        height: 1.12rem;
        padding: 0 0.3rem 0 0.3rem;
        font-size: 0.24rem;
        color: #333333;
        background: #ffffff;
        &::after {
          content: "";
          display: block;
          position: absolute;
          bottom: 0;
          right: 0;
          width: 100%;
          height: 0.02rem;
          background: #d2d2d2;
          transform: scaleY(0.5);
        }
        &:last-child::after {
          display: none;
        }
        .contents {
          display: flex;
          justify-content: space-between;
          align-items: center;
          height: 100%;
          .baseinfo {
            display: flex;
            .iconimg {
              width: 0.72rem;
              height: 0.72rem;
              border-radius: 50%;
            }
            .types {
              flex: 1;
              display: flex;
              flex-direction: column;
              justify-content: center;
              margin-left: 0.2rem;
              .type {
                margin-bottom: 0.1rem;
              }
              .time {
                font-size: 0.24rem;
                color: #666666;
              }
            }
          }
          .unuse {
            font-size: 0.24rem;
            color: #D44743;
          }
          .use {
            font-size: 0.24rem;
            color: #999999;
          }
        }
      }
    }
    .no-datas {
      position: fixed;
      width: 100%;
      // top: 1.92rem;
      top: 2.40rem;
      bottom: 0;
      height: 100%;
      text-align: center;
      background: #ffffff;
      img {
        display: block;
        width: 4.15rem;
        height: 3.22rem;
        margin: 0 auto;
        padding-top: 0.8rem;
      }
      .desc {
        margin-top: 0.3rem;
        font-size: 0.22rem;
        color: #919191;
      }
    }
    .vux-datetime {
      display: none;
    }
    .record-type {
      .header {
        position: relative;
        height: 0.8rem;
        line-height: 0.8rem;
        font-size: 0.32rem;
        color: #333333;
        text-align: center;
        &::after {
          content: "";
          display: block;
          position: absolute;
          bottom: 0;
          right: 0;
          width: 100%;
          height: 0.02rem;
          background: #d2d2d2;
          transform: scaleY(0.5);
        }
        .icon-del {
          position: absolute;
          right: 0.2rem;
          top: 0;
          width: 0.3rem;
          height: 0.3rem;
          padding: 0.25rem;
          background-image: url('../../../assets/imgs/icon-close@2x.png');
          background-size: 0.3rem 0.3rem;
          background-position: 50% 50%;
          background-repeat: no-repeat;
        }
      }
      .types {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.3rem 0.2rem;
        .state {
          width: 32.3%;
          height: 0.8rem;
          line-height: 0.8rem;
          font-size: 0.28rem;
          color: #333333;
          text-align: center;
          border: 1px solid #cd6c69;
          border-radius: 0.06rem;
          box-sizing: border-box;
          &.on {
            color: #ffffff;
            background: #d44743;
          }
        }
      }
      .btn {
        width: 100%;
        height: 1rem;
        line-height: 1rem;
        font-size: 0.34rem;
        color: #ffffff;
        text-align: center;
        background: #d44743;
      }
    }
  }
</style>
